package com.caird.pmp.iot.message.impl;

import com.caird.pmp.iot.message.MessageBase;
import io.netty.buffer.ByteBuf;
import lombok.Data;

@Data
public class ServerMessage81E0 extends MessageBase {


    public ServerMessage81E0() {
        id = (short)0x81E0;
    }

    @Override
    public int getMaxBodyLength() {
        return 128;
    }

    @Override
    public boolean encodeBody(ByteBuf raw) {
        if (null == result) {
            return false;
        }

        //结果码
        raw.writeByte(result);

        if (0 == result) {
            //注册码
            if (null == license || 32 != license.length()) {
                return false;
            }
            raw.writeBytes(license.getBytes());

            //终端ID
            if (null == sn || 7 != sn.length()) {
                return false;
            }
            raw.writeBytes(sn.getBytes());

            //终端ID
            if (null == fullSimNo || fullSimNo.length() < 11 || fullSimNo.length() > 16) {
                return false;
            }
            raw.writeBytes(fullSimNo.getBytes());
            for (int i=0; i<16 - fullSimNo.length(); i++) {
                raw.writeByte(0);
            }

            //车型ID
            raw.writeInt(0);

            //车架号
            if (null == vin || 17 != vin.length()) {
                return false;
            }
            raw.writeBytes(vin.getBytes());
            for (int i=0; i<20 - vin.length(); i++) {
                raw.writeByte(0);
            }
        } else {
            for (int i=0; i<79; i++) {
                raw.writeByte(0);
            }
        }

        return true;
    }

    //结果码
    private Byte result;

    //设备license
    private String license;

    //设备序列号
    private String sn;

    //卡号
    private String fullSimNo;

    //车型id
    private Integer vid;

    //车身序列号
    private String vin;
}
